package com.newtouch.krd_app;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.hjq.http.EasyConfig;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.http.request.HttpRequest;
import com.hjq.toast.ToastUtils;
import com.newtouch.krd_app.http.ReleaseServer;
import com.newtouch.krd_app.http.model.RequestHandler;
import com.newtouch.krd_app.util.BaseUrlUtil;
import com.newtouch.krd_app.util.CrashHandler;
import com.newtouch.krd_app.util.SPUtils;


import java.util.HashMap;

import okhttp3.OkHttpClient;
/**
 *    author : TQ
 *    time   : 2023/02/13
 *    desc   : 初始应用入口
 */
public class KrdApplication extends Application {
    private final String LOG_TAG = "KrdApplication";
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化-异常捕获(设置该CrashHandler为程序的默认处理器)
        CrashHandler unCeHandler = CrashHandler.getInstance();
        unCeHandler.init(this);
        Thread.setDefaultUncaughtExceptionHandler(unCeHandler);
//        initTBS();
        ToastUtils.init(this);
        HttpInit();

    }

    /**
     * 初始化腾讯浏览器内核
     */
    private void initTBS() {
//        HashMap<String, Object> map = new HashMap<>();
//        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
//        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
//        QbSdk.initTbsSettings(map);
//
//        QbSdk.setDownloadWithoutWifi(true);
//        Log.d(LOG_TAG, "开始加载内核");
//        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
//            @Override
//            public void onCoreInitFinished() {
//
//            }
//
//            @Override
//            public void onViewInitFinished(boolean b) {
//                if (b){
//                    Log.d(LOG_TAG, "内核加载成功");
//                }else{
//                    Log.d(LOG_TAG, "内核加载失败");
//                }
//            }
//        });
//        QbSdk.setTbsListener(new TbsListener() {
//            @Override
//            public void onDownloadFinish(int i) {
//                Log.d(LOG_TAG, "下载完成 progress = " + i);
//            }
//
//            @Override
//            public void onInstallFinish(int i) {
//                Log.d(LOG_TAG, "正在安装内核 progress = " + i);
//            }
//
//            @Override
//            public void onDownloadProgress(int i) {
//                Log.d(LOG_TAG, "已经下载 progress = " + i);
//            }
//        });
//        boolean needDownload = TbsDownloader.needDownload(this, TbsDownloader.DOWNLOAD_OVERSEA_TBS);
//        Log.e(LOG_TAG, "onCreate: "+needDownload );
//        if (needDownload) {
//            TbsDownloader.startDownload(this);
//        }

    }

    /**
     * http 框架初始化
     */
    private void HttpInit() {
        BaseUrlUtil.getInstance().init(getApplicationContext());
        // 网络请求框架初始化
        IRequestServer server = new ReleaseServer();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();

        EasyConfig.with(okHttpClient)
                // 是否打印日志
                .setLogEnabled(true)
                // 设置服务器配置
                .setServer(server)
                // 设置请求处理策略
                .setHandler(new RequestHandler(this))
                // 设置请求参数拦截器
                .setInterceptor(new IRequestInterceptor() {
                    @Override
                    public void interceptArguments(@NonNull HttpRequest<?> httpRequest,
                                                   @NonNull HttpParams params,
                                                   @NonNull HttpHeaders headers) {
                        headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
                    }
                })
                // 设置请求重试次数
                .setRetryCount(1)
                // 设置请求重试时间
                .setRetryTime(2000)
                // 添加全局请求参数
//                .addParam("token", "6666666")
                // 添加全局请求头
                //.addHeader("date", "20191030")
                .into();
    }
}
